﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using _22yards.Core;

namespace _22yards.Core
{
    public class Team
    {
        public static short getRegionIDFromTeamID(int CompleteTeamID)
        {
            return (short)(CompleteTeamID / 1000000);
        }

        public static string getRegionNameFromTeamID(int CompleteTeamID)
        {
            int regionID = getRegionIDFromTeamID(CompleteTeamID);

            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Teams\Teams.xml");
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "REGION")
                        {
                            string IDfromXML=xmlReader.GetAttribute(0);
                            if (IDfromXML == regionID.ToString())
                                return xmlReader.GetAttribute(1);
                        }
                        break;
                }
            }
            return "INVALID REGION ID";
        }

        public static short getCountryIDFromTeamID(int CompleteTeamID)
        {
            int tmp = CompleteTeamID / 10000;
            return (short)(tmp%100);
        }

        public static string getCountryNameFromTeamID(int CompleteTeamID)
        {
            int countryID = getCountryIDFromTeamID(CompleteTeamID);

            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Teams\Teams.xml");
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "COUNTRY")
                        {
                            string IDfromXML = xmlReader.GetAttribute(0);
                            if (IDfromXML == countryID.ToString())
                                return xmlReader.GetAttribute(1);
                        }
                        break;
                }
            }
            return "INVALID COUNTRY ID";
        }
        
        public static int getTeamTypeIDFromTeamID(int CompleteTeamID)
        {
            int tmp = CompleteTeamID / 100;
            return (short)(tmp % 10000);
        }

        public static string getTeamTypeNameFromTeamID(int CompleteTeamID)
        {
            int teamTypeID = getTeamTypeIDFromTeamID(CompleteTeamID);

            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Teams\Teams.xml");
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "TYPE")
                        {
                            string IDfromXML = xmlReader.GetAttribute(0);
                            if (IDfromXML == teamTypeID.ToString())
                                return xmlReader.GetAttribute(1);
                        }
                        break;
                }
            }
            return "INVALID TYPE ID";

        }

        public static string getTeamNamefromTeamID(int CompleteTeamID)
        {
            XmlTextReader xmlReader = new XmlTextReader(@"..\..\Teams\Teams.xml");
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "TEAM")
                        {
                            string IDfromXML = xmlReader.GetAttribute(0);
                            if (IDfromXML == CompleteTeamID.ToString())
                                return xmlReader.Value;
                        }
                        break;
                }
            }
            return "INVALID TEAM ID";

        }


        public decimal NetMoney { get; set; }
        public int TeamNumber { get; set; }
        public string TeamName { get; set; }
        public List<cPlayer> playersInSquad;
        public bool TossWinner { get; set; }
        public bool IsUserSelected { get; set; }

        public void initTeam(int __teamNumber)
        {
            playersInSquad = new List<cPlayer>();
            //Code to fetch team members into team
            throw new NotImplementedException();
        }

        //Should be called after loadPlayerinSquad is called
        public string markPlayerInPlaying11(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.InPlaying11 = true;
                    break;
                }

            }

            return "Success";
        }

        //Should be called after loadPlayerinSquad is called
        public string UnmarkPlayerInPlaying11(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.InPlaying11 = false;
                    break;
                }

            }

            return "Success";
        }

        public string markPlayerAsExtra(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.Is12thMan = true;
                    break;
                }

            }

            return "Success";
        }

        public string UnmarkPlayerAsExtra(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.Is12thMan = false;
                    break;
                }
            }
            return "Success";
        }

        public string markPlayerAsWicketKeeper(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.IsWicketKeeper = true;
                    break;
                }
            }
            return "Success";
        }

        public string UnmarkPlayerAsWicketKeeper(cPlayer player)
        {
            if (playersInSquad.Count < 15)
                return "Squad contains " + playersInSquad.Count + " players. Please select 15 only";

            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.PlayerID == player.PlayerID)
                {
                    cplr.IsWicketKeeper = false;
                    break;
                }
            }
            return "Success";
        }

        public void loadPlayerInSquad(cPlayer player) //Can load upto 15 players
        {
            if(playersInSquad.Count <15)
                playersInSquad.Add(player);
        }

        public void removePlayerFromSquad(cPlayer player)
        {
            if (playersInSquad.Count > 0)
            {
                foreach (cPlayer cplr in playersInSquad)
                {
                    if (cplr.PlayerID == player.PlayerID)
                    {
                        playersInSquad.Remove(cplr);
                        break;
                    }

                }
            }
        }

        public short GetTeamCurrentBattingForm()
        {
            short iBatForm = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11)
                {
                    ctr++;
                    iBatForm += cplr.CurrentBattingForm;
                }
            }
            return (short)(iBatForm / ctr);
        }

        public short GetTeamCurrentBowlingForm()
        {
            short iBowlForm = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11)
                {
                    ctr++;
                    iBowlForm += cplr.CurrentBowlingForm;
                }
            }
            return (short)(iBowlForm/ctr);
        }

        public short GetTeamCurrentFieldingForm()
        {
            short iFieldForm = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11 || cplr.Is12thMan)
                {
                    ctr++;
                    iFieldForm += cplr.CurrentFieldingForm;
                }
            }
            return (short)(iFieldForm / ctr);
        }

        public short GetTeamCurrentOverallForm()
        {
            return (short)((GetTeamCurrentBattingForm() + GetTeamCurrentBowlingForm() + GetTeamCurrentFieldingForm()) / 3);
        }

        public short GetTeamBattingPoints()
        {
            short iBatPoints = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11)
                {
                    ctr++;
                    iBatPoints += cplr.BattingSkillPoints;
                }
            }
            return (short)(iBatPoints / ctr);
        }

        public short GetTeamBowlingPoints()
        {
            short iBowlPoints = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11)
                {
                    ctr++;
                    iBowlPoints += cplr.BowlingSkillPoints;
                }
            }
            return (short)(iBowlPoints / ctr);
        }

        public short GetTeamFieldingPoints()
        {
            short iFieldPoints = 0;
            short ctr = 0;
            foreach (cPlayer cplr in playersInSquad)
            {
                if (cplr.InPlaying11 || cplr.Is12thMan)
                {
                    ctr++;
                    iFieldPoints += cplr.FieldingSkillPoints;
                }
            }
            return (short)(iFieldPoints / ctr);
        }

        public short GetTeamOverallPoints()
        {
            return (short)((GetTeamBattingPoints() + GetTeamBowlingPoints() + GetTeamFieldingPoints()) / 3);
        }
    }
}
